package com.sl.hx.shop.ui.home.presenter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.CentralNavigationList;
import com.sl.hx.shop.bean.HomeBannerBean;
import com.sl.hx.shop.bean.HomeGoodBuy;
import com.sl.hx.shop.bean.HomeRecommend;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.activity.FlashSaleActivity;
import com.sl.hx.shop.ui.home.activity.HotListActivity;
import com.sl.hx.shop.ui.home.activity.SlectStoreActivity;
import com.sl.hx.shop.ui.home.activity.WebAvtivity;
import com.sl.hx.shop.ui.home.contacts.HomeContacts;
import com.sl.hx.shop.utils.SPUtils;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.List;

import io.reactivex.Observable;

public class HomePresenter extends BasePresenter<HomeContacts.view> implements HomeContacts.presenter {

    private Context mContext;


    public HomePresenter(Context context) {
        this.mContext=context;

    }

    public void getBanner() {
        Observable observable = ApiFactory.getInstance().getBanner(ApiRetrofit.getCacheControl(),"1");
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<HomeBannerBean>>(mContext) {
            @Override
            protected void _onNext(List<HomeBannerBean> homeBannerBeans) {
                getView().ShowBanner(homeBannerBeans);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }

    //bannner点击事件
    public void BannerClick(HomeBannerBean BeanPosition, List<HomeBannerBean> homeBannerBeans, int position) {
        indexCount(1,BeanPosition.getId());
        if (!TextUtils.isEmpty(BeanPosition.getUrl())) {
            //跳转H5
            Intent intent = new Intent(mContext, WebAvtivity.class);
            intent.putExtra("webviewUrl", BeanPosition.getUrl());
            intent.putExtra("webviewTitle",BeanPosition.getName());
            mContext.startActivity(intent);

        } else if (BeanPosition.getFavorites_id() > 0) {
            //跳选品库
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",BeanPosition.getFavorites_id());
            intent.putExtra("slectStore_name",BeanPosition.getName());
            mContext.startActivity(intent);
        } else {
            Toast.makeText(mContext, "暂无数据", Toast.LENGTH_SHORT).show();
        }
    }

    //中部导航点击
    public void NavigationClick(CentralNavigationList item) {
        if (!TextUtils.isEmpty(item.getUrl())){
            //跳h5
            Intent intent = new Intent(mContext,WebAvtivity.class);
            intent.putExtra("webviewUrl",item.getUrl());
            intent.putExtra("webviewTitle",item.getName());
            mContext.startActivity(intent);

        }else if (item.getFavorites_id()>0){
            //跳选品库
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",item.getFavorites_id());
            intent.putExtra("slectStore_name",item.getName());
            mContext.startActivity(intent);
        }
    }

    public void getNavigationList() {
        Observable observable = ApiFactory.getInstance().getCentralNavigation(ApiRetrofit.getCacheControl(),"2");
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<CentralNavigationList>>(mContext) {
            @Override
            protected void _onNext(List<CentralNavigationList> NavigationList) {
                getView().ShowNavigationList(NavigationList);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }



    public void getRecommend() {
        Observable observable = ApiFactory.getInstance().getRecommend(ApiRetrofit.getCacheControl());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<HomeRecommend>>(mContext) {
            @Override
            protected void _onNext(List<HomeRecommend> recommendList) {
                getView().ShowRecommend(recommendList);
            }
            @Override
            protected void _onError(String message) {
                getView().ShowRecommendFail();
            }
        }, false, getView(), FragmentEvent.DESTROY);
    }

    //1：热销爆款
    public void getHotsell(int i) {
        Observable observable = ApiFactory.getInstance().getGoodBuy(ApiRetrofit.getCacheControl(),i);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<HomeGoodBuy>(mContext) {
            @Override
            protected void _onNext(HomeGoodBuy goodBuyList) {
                getView().ShowHotsell(goodBuyList);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }
    //2：热销排行榜
    public void getHotlist(int i) {
        Observable observable = ApiFactory.getInstance().getGoodBuy(ApiRetrofit.getCacheControl(),i);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<HomeGoodBuy>(mContext) {
            @Override
            protected void _onNext(HomeGoodBuy goodBuyList) {
                getView().ShowHotlist(goodBuyList);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }

    //3：女装上新
    public void getNewWoman(int i) {
        Observable observable = ApiFactory.getInstance().getGoodBuy(ApiRetrofit.getCacheControl(),i);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<HomeGoodBuy>(mContext) {
            @Override
            protected void _onNext(HomeGoodBuy goodBuyList) {
                getView().ShowNewWoman(goodBuyList);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }

    //4：限时抢购
    public void getFlashSale(int i) {
        Observable observable = ApiFactory.getInstance().getGoodBuy(ApiRetrofit.getCacheControl(),i);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<HomeGoodBuy>(mContext) {
            @Override
            protected void _onNext(HomeGoodBuy goodBuyList) {
                getView().ShowFlashSale(goodBuyList);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }

    //5：9.9包邮专区
    public void getFreeShop(int i) {
        Observable observable = ApiFactory.getInstance().getGoodBuy(ApiRetrofit.getCacheControl(),i);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<HomeGoodBuy>(mContext) {
            @Override
            protected void _onNext(HomeGoodBuy goodBuyList) {
                getView().ShowFreeShop(goodBuyList);
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }

    //限时抢购点击
    public void FlashSaleClick(HomeGoodBuy goodBuyList) {
        indexCount(3,goodBuyList.getId());
        if (!TextUtils.isEmpty(goodBuyList.getUrl())){
            //跳h5
            Intent intent = new Intent(mContext,WebAvtivity.class);
            intent.putExtra("webviewUrl",goodBuyList.getUrl());
            intent.putExtra("webviewTitle",goodBuyList.getName());
            mContext.startActivity(intent);

        }else if (goodBuyList.getFavorites_id()>0){
            //跳选品库页面(与搜索结果页相同)
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",goodBuyList.getFavorites_id());
            intent.putExtra("slectStore_name",goodBuyList.getName());
            mContext.startActivity(intent);
        }else {
            //跳限时抢购页
            Intent intent = new Intent(mContext, FlashSaleActivity.class);
            mContext.startActivity(intent);
        }
    }

    //热销爆款点击
    public void HotsellClick(HomeGoodBuy goodBuyList) {
        indexCount(3,goodBuyList.getId());
        if (!TextUtils.isEmpty(goodBuyList.getUrl())){
            //跳h5
            Intent intent = new Intent(mContext,WebAvtivity.class);
            intent.putExtra("webviewUrl",goodBuyList.getUrl());
            intent.putExtra("webviewTitle",goodBuyList.getName());
            mContext.startActivity(intent);

        }else if (goodBuyList.getFavorites_id()>0){
            //跳选品库
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",goodBuyList.getFavorites_id());
            intent.putExtra("slectStore_name",goodBuyList.getName());
            mContext.startActivity(intent);
        }
    }

    //热销排行榜点击
    public void HotlistClick(HomeGoodBuy goodBuyList) {
        indexCount(3,goodBuyList.getId());
        if (!TextUtils.isEmpty(goodBuyList.getUrl())){
            //跳h5
            Intent intent = new Intent(mContext,WebAvtivity.class);
            intent.putExtra("webviewUrl",goodBuyList.getUrl());
            intent.putExtra("webviewTitle",goodBuyList.getName());
            mContext.startActivity(intent);

        }else if (goodBuyList.getFavorites_id()>0){
            //跳选品库
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",goodBuyList.getFavorites_id());
            intent.putExtra("slectStore_name",goodBuyList.getName());
            mContext.startActivity(intent);
        }else {
            //跳热销排行榜页
            Intent intent = new Intent(mContext, HotListActivity.class);
            mContext.startActivity(intent);
        }
    }

    //女装上新点击
    public void NewWomanClick(HomeGoodBuy goodBuyList) {
        indexCount(3,goodBuyList.getId());
        if (!TextUtils.isEmpty(goodBuyList.getUrl())){
            //跳h5
            Intent intent = new Intent(mContext,WebAvtivity.class);
            intent.putExtra("webviewUrl",goodBuyList.getUrl());
            intent.putExtra("webviewTitle",goodBuyList.getName());
            mContext.startActivity(intent);

        }else if (goodBuyList.getFavorites_id()>0){
            //跳选品库
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",goodBuyList.getFavorites_id());
            intent.putExtra("slectStore_name",goodBuyList.getName());
            mContext.startActivity(intent);
        }
    }

    //包邮专区点击
    public void FreeShopClick(HomeGoodBuy goodBuyList) {
        indexCount(3,goodBuyList.getId());
        if (!TextUtils.isEmpty(goodBuyList.getUrl())){
            //跳h5
            Intent intent = new Intent(mContext,WebAvtivity.class);
            intent.putExtra("webviewUrl",goodBuyList.getUrl());
            intent.putExtra("webviewTitle",goodBuyList.getName());
            mContext.startActivity(intent);

        }else if (goodBuyList.getFavorites_id()>0){
            //跳选品库
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",goodBuyList.getFavorites_id());
            intent.putExtra("slectStore_name",goodBuyList.getName());
            mContext.startActivity(intent);
        }
    }


    /**
     * 首页统计
     * @param layer 1:顶部导航，2：中部导航，3：好惠买，4：今日推荐
     * @param position  位置id
     */
    public void indexCount(int layer,int position) {
        Observable observable = ApiFactory.getInstance().indexCount(layer,SPUtils.getInstance().getInt(SPUKey.TOKENID),position, AppConfig.CHANNEL);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }
}
